<template>
  <div class="account-selector-root">
    <div class="current-account" v-if="selectedAccount" @click="dialogVisible = true">
      <img :src="selectedAccount.avatar_url" class="avatar" alt="头像" />
      <span class="nickname">{{ selectedAccount.nick_name }}</span>
      <el-icon class="arrow"><ArrowDown /></el-icon>
    </div>
    <el-dialog v-model="dialogVisible" title="切换微信账号" width="380px" append-to-body class="account-dialog">
      <template #header>
        <div class="dialog-header">
          <span>切换微信账号</span>
        </div>
      </template>
      <div class="account-list">
        <div
          v-for="account in accounts"
          :key="account.wx_id"
          :class="['account-item', { selected: account.wx_id === selectedAccount?.wx_id }]"
          @click="handleSelect(account)"
          :title="account.nick_name + (account.isOnline ? '（在线）' : '（离线）')"
        >
          <img :src="account.avatar_url" class="avatar" alt="头像" />
          <div class="info">
            <div class="name">{{ account.nick_name }}</div>
            <div class="status" :class="{ online: account.isOnline }">
              {{ account.isOnline ? '在线' : '离线' }}
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { ArrowDown } from '@element-plus/icons-vue';
import { getWxListByaccount } from '@/api/wechat';

interface Account {
  wx_id: string;
  nick_name: string;
  avatar_url: string;
  isOnline?: boolean;
}

const accounts = ref<Account[]>([]);
const selectedAccount = ref<Account | undefined>(undefined);
const dialogVisible = ref(false);

const fetchAccounts = async () => {
  const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}');
  const account = userInfo.username;
  if (!account) return;
  const res = await getWxListByaccount({ account });
  if (res && Array.isArray(res.data)) {
    accounts.value = res.data;
    if (accounts.value.length > 0) {
      selectedAccount.value = accounts.value[0];
    }
  }
};

function handleSelect(account: Account) {
  selectedAccount.value = account;
  dialogVisible.value = false;
}

function handleAddAccount() {
  // 这里可以弹出添加账号的逻辑，或跳转到添加账号页面
  // 目前仅弹出提示
  window.$message?.info('请在设置页面添加新账号');
}

onMounted(fetchAccounts);
</script>

<style scoped lang="scss">
.account-selector-root {
  width: 100%;
  .current-account {
    display: flex;
    align-items: center;
    padding: 12px 20px;
    border-top: 1px solid #f0f0f0;
    cursor: pointer;
    background: #f8f9fa;
    transition: background 0.2s;
    &:hover {
      background: #e6f7ff;
    }
    .avatar {
      width: 32px;
      height: 32px;
      border-radius: 50%;
      object-fit: cover;
      margin-right: 10px;
      border: 2px solid #fff;
      box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04);
    }
    .nickname {
      font-size: 15px;
      font-weight: 500;
      color: #222;
      flex: 1;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .arrow {
      margin-left: 6px;
      color: #bbb;
      font-size: 18px;
    }
  }
}
.account-dialog {
  :deep(.el-dialog__body) {
    padding: 0 10px 18px 10px;
  }
  .dialog-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 16px;
    font-weight: 600;
    padding: 0 4px 0 0;
  }
}
.account-list {
  max-height: 320px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 8px;
  padding-right: 2px;
  .account-item {
    display: flex;
    align-items: center;
    padding: 10px 14px;
    border-radius: 10px;
    cursor: pointer;
    background: #f6f8fa;
    transition: box-shadow 0.2s, background 0.2s, border 0.2s;
    border: 2px solid transparent;
    min-width: 180px;
    box-shadow: 0 1px 4px rgba(44, 62, 80, 0.04);
    &:hover {
      background: #e6f7ff;
      box-shadow: 0 2px 8px rgba(44, 62, 80, 0.1);
      border-color: #409eff;
    }
    &.selected {
      border-color: #44cb84;
      background: #e8f9f0;
      .name {
        font-weight: 700;
        color: #44cb84;
      }
    }
    .avatar {
      width: 36px;
      height: 36px;
      border-radius: 50%;
      object-fit: cover;
      margin-right: 12px;
      border: 2px solid #fff;
      box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04);
    }
    .info {
      display: flex;
      flex-direction: column;
      .name {
        font-weight: 600;
        font-size: 16px;
        color: #222;
        margin-bottom: 2px;
      }
      .status {
        font-size: 13px;
        color: #aaa;
        &.online {
          color: #44cb84;
          font-weight: 600;
        }
      }
    }
  }
}
</style>

export default {}; 